package customer

import (
	"context"
	"fmt"
	"sieve_admin_server_userpass/app/api/util"
	"sieve_admin_server_userpass/app/contextx"
	"sieve_admin_server_userpass/app/schema"
	serviceCustomer "sieve_admin_server_userpass/app/service/customer"
	"sieve_admin_server_userpass/lib/log"

	"github.com/google/wire"
	"sieve_admin_server_userpass/app/server"
)

var (
	_                util.API = (*DailyRechargeAPI)(nil)
	DailyRechargeSet          = wire.NewSet(wire.Struct(new(DailyRechargeAPI), "*"))
)

type DailyRechargeAPI struct {
	util.EmptyAPI
	DailyRechargeSrv *serviceCustomer.DailyRechargeSrv
}

func (a *DailyRechargeAPI) Cron() {

	_ = a.DailyRechargeSrv.Cron()
}

func (a *DailyRechargeAPI) GetApis() []*server.BatchModTree {
	return []*server.BatchModTree{
		{
			Obj:   a,
			Name:  "dailyRecharge",
			Title: "日充值统计", Perms: []*server.Perm{
				{Name: "query", Title: "搜索"},
				{Name: "export", Title: "导出列表"},
			},
			Order: 6,
		},
	}
}

func (a *DailyRechargeAPI) API_query_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("date"),           // eg:2023-03-01,2023-03-31
				contextx.NewVar("leader_id"),      // eg:2023-03-01,2023-03-31
				contextx.NewVar("overview", true), // 是否是首页展示的数据
			},
		)

		p = schema.PaginationParam{
			PageIndex: util.ToInt(ctx.Args.Get("page_index")),
			PageSize:  util.ToInt(ctx.Args.Get("page_size")),
		}
	)
	// reqArgs["leader_id"] = ctx.Uid
	return a.DailyRechargeSrv.Query(context.Background(), uint64(ctx.Uid), reqArgs, p)
}

func (a *DailyRechargeAPI) API_export_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("ids"),  // recharge 表的主键ID，多个 用 "|" 拼接
				contextx.NewVar("seqs"), // recharge 表的主键ID，多个 用 "|" 拼接
			},
		)
	)

	return a.DailyRechargeSrv.Export(context.Background(), uint64(ctx.Uid), reqArgs, func(fileName string) {

		log.Info(fmt.Sprintf("export file %v", fileName))
		// http.ServeFile(ctx.Http.ResponseWriter, ctx.Http.Request, fileName)
	})
}
